﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;

namespace WinRpaLib
{
    public class RpaHelper
    {
        public RpaResult DoAction(RpaCommand cmd)
        {
            RpaResult result = _interDoAction(cmd);

            return result;
        }


        private RpaResult _interDoAction(RpaCommand cmd)
        {
            RpaResult result = new RpaResult();

            AutomationElement element = UiElementFinder.FindElementBySelecter(cmd.UISltCondition);

            if (cmd.Action == "Press")
            {
                InvokeElement(element);
            }
            else if (cmd.Action == "Input")
            {
                EditPattern_Input(element, cmd.Parameter);
            }

            return result;
        }

        private void InvokeElement(AutomationElement element)
        {
            try
            {
                // Attempt to invoke (press) the element
                InvokePattern invokePattern = element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                if (invokePattern != null)
                {
                    invokePattern.Invoke();
                    Console.WriteLine("Element invoked successfully.");
                }
                else
                {
                    Console.WriteLine("Element is not invocable.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error invoking element: {ex.Message}");
            }
        }


        private void EditPattern_Input(AutomationElement element, string text)
        {
            // 获取编辑控件模式
            ValuePattern editPattern = element.GetCurrentPattern(System.Windows.Automation.ValuePattern.Pattern) as ValuePattern;
            if (editPattern != null)
            {
                // 写入内容
                editPattern.SetValue(text);
            }
            else
            {
                Console.WriteLine("无法获取编辑控件模式。");
            }
        }
    }
    public class RpaCommand
    {
        public RpaCommand()
        {
        }
        public RpaCommand(string title, string action, string uISltCondition, string parameter)
        {
            Title = title;
            Action = action;
            UISltCondition = uISltCondition;
            Parameter = parameter;
        }
        public string Title { get; set; }
        public string Action { get; set; }
        public string UISltCondition { get; set; }

        public string Parameter { get; set; }

        public override String ToString()
        {
            return String.Format("【Title】:{0}\n【Action】:{1}\n【Selecter】:{2}\n【Parameter】:{3}\n", Title, Action, UISltCondition, Parameter);
        }
        public void Parser(String text)
        {
            string[] ss = text.Split('\n');
            foreach (var s in ss)
            {
                if (s.StartsWith("【Title】"))
                {
                    Title = s.Substring(8);
                }
                if (s.StartsWith("【Action】"))
                {
                    Action = s.Substring(9);
                }
                if (s.StartsWith("【Selecter】"))
                {
                    UISltCondition = s.Substring(11);
                }
                if (s.StartsWith("【Parameter】"))
                {
                    Parameter = s.Substring(11);
                }
            }

            return ;
        }
    }

    public class RpaResult
    {

    }
}
